package com.ccx.coachsite.task;

import org.json.JSONObject;

import android.content.Context;

import com.ccx.coachsite.bus.Account;
import com.ccx.coachsite.bus.AccountMgr;
import com.ccx.coachsite.bus.SharedKey;
import com.ccx.coachsite.http.HttpCallBack;
import com.ccx.coachsite.http.HttpRequest;
import com.ccx.coachsite.http.UrlConfig;
import com.ccx.coachsite.util.Log;
import com.ccx.coachsite.util.SharedPreferencesUtil;
import com.ccx.coachsite.util.StringUtil;

public class OwerInfoTask extends TaskBase 
{	
	private Account account;
	
	public OwerInfoTask(HttpCallBack<?> iCall, Context context, Account account) 
	{
		super(iCall, context);
		mTaskType = TaskType.TaskTypeOwerInfo;
		this.account = account;
	}

	@Override
	protected String genUrl(HttpRequest request) {
		return UrlConfig.getUrlByKey(UrlConfig.OwerInfoUrlKey) + "?userId="+mUserId+"&userType="+mUserType;
	}

	@Override
	protected void onDataParse(String responseString) 
	{
		try {
			JSONObject jsonObject = new JSONObject(responseString);
			JSONObject respsonObject = jsonObject.getJSONObject("response");
			
			if(!respsonObject.isNull("nickName"))
				account.setUsername(respsonObject.getString("nickName"));
			if(!respsonObject.isNull("username"))
				account.setUsername(respsonObject.getString("username"));
			if(!respsonObject.isNull("qq"))
				account.setQq(respsonObject.getString("qq"));
			String icon = "";
			if(!respsonObject.isNull("icon"))
				icon = respsonObject.getString("icon");
			if(!StringUtil.isEmpty(icon))
			{
				icon = UrlConfig.getImageBaseUrl() + icon;
			}
			account.setOwerIcon(icon);
			if(!respsonObject.isNull("id"))
				account.setUserId(respsonObject.getInt("id"));
			if(!respsonObject.isNull("userId"))
				account.setUserId(respsonObject.getInt("userId"));
			if(!respsonObject.isNull("mobile"))
				account.setMobile(respsonObject.getString("mobile").trim());
			if(!respsonObject.isNull("phone"))
				account.setMobile(respsonObject.getString("phone").trim());
			
			account.setPassword(SharedPreferencesUtil.getValue(mContext,
					SharedKey.Shared_Password_Key));
			
			if(!respsonObject.isNull("collection"))
				account.setOwerCollectCount(respsonObject.getInt("collection"));
			
			if(!respsonObject.isNull("appNum"))
				account.setOwerAppCollectCount(respsonObject.getInt("appNum"));
			
			if(!respsonObject.isNull("tutorialNum"))
				account.setOwerTurCollectCount(respsonObject.getInt("tutorialNum"));
			/*
			account.setOwerRole(respsonObject.getString("roleName"));
			account.setOwerPackage(respsonObject.getString("package"));
			account.setOwerStar(respsonObject.getInt("star"));
			account.setOwerFlow(respsonObject.getString("gprs_used"));
			account.setOwerCollectCount(respsonObject.getInt("collectNum"));
			account.setOwerFriendCount(respsonObject.getInt("friendNum"));
			*/
			AccountMgr.saveAccountInfo(mContext, account);
		} catch (Exception e) {
			Log.e(e.getMessage(), e);
		} finally {
		}
	} 

}
